package com.loe.ui.view_other

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.Keep
import com.loe.mvp.ext_java.delaySafe
import com.loe.mvp.ext_java.safe
import com.loe.ui.*
import com.loe.ui.api.IUiView
import com.loe.ui.api.ViewGroupApi
import com.loe.ui.page.UiPage
import com.loe.ui.view.UiLinearLayout
import com.quickjs.JSArray
import com.quickjs.JSObject
import kotlin.collections.ArrayList

@Keep
class UiTabView : UiLinearLayout
{
    constructor(context: Context) : super(context)

    override val jsObject by lazy {
        val view = this
        object : ViewGroupApi(this)
        {
            @JavascriptInterface
            fun getIndex(child: JSObject): Int = view.index
        }.jsObject
    }

    var tabLayout: LinearLayout? = null
        private set

    var tabLine: View? = null
        private set

    var childSize = 0
        private set

    val list: ArrayList<View> = ArrayList()

    override fun setUI(ui: JSObject, uiPage: UiPage?): View
    {
        this.uiPage = uiPage
        orientation = VERTICAL

        // click
        ui.gotHasBoolean("isClick") { setClick(it) }

        // gravity
        ui.gotHasGravity("gravity") { gravity = it }

        // tabLineRate
        ui.gotHasDouble("tabLineRate") { tabLineRate = it }

        // tabLineSticky
        ui.gotHasBoolean("tabLineSticky") { tabLineSticky = it }

        // 添加tabLayout
        tabLayout = LinearLayout(context)
        tabLayout?.orientation = HORIZONTAL
        val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        params.weight = 1f
        addView(tabLayout, params)

        // 递归子元素到tabLayout
        list.clear()
        ui.gotHasArray("childs")
        { childs ->
            childs.forEach<JSObject>
            {
                uiPage?.createUI(it, tabLayout)?.let()
                { childView ->
                    addChild(childView)
                }
            }
            childSize = childs.length()
        }

        // tabLine
        ui.gotHasObject("tabLine")
        {
            tabLine = uiPage?.createUI(it, this)
        }

        // onItemSelect
        ui.gotHasFunction("onItemSelect")
        { callback ->
            onItemSelect =
                { childView, isSelect ->
                    safe()
                    {
                        childView as IUiView
                        callback.call(
                            jsObject, JSArray(jsContext)
                                .push(childView.jsObject)
                                .push(isSelect)
                        )
                        if (isSelect) childView.onSelect?.invoke()
                    }
                }
        }

        // select
        ui.gotHasInt("select")
        {
            select(it, true)
        }

        return this
    }

    private var onItemSelect: ((childView: View, isSelect: Boolean) -> Unit)? = null

    var onItemSelected: ((i: Int) -> Unit)? = null

    private fun addChild(childView: View)
    {
        list.add(childView)
        childView.setOnClickListener()
        {
            select(it)
        }
    }

    /** 滑动线条长度比例 */
    var tabLineRate = 0.5

    /** 滑动线条是否黏性 */
    var tabLineSticky = true

    /** 当前选项下标 */
    var index = -1
        private set

    fun select(childView: View?, isStatic: Boolean = false)
    {
        index = -1
        list.forEachIndexed()
        { i, view ->
            val isSelect = view == childView
            if (isSelect)
            {
                index = i
                if(!isStatic) onItemSelected?.invoke(i)
            }
            onItemSelect?.invoke(view, isSelect)
        }
    }

    fun select(i: Int, isStatic: Boolean = false)
    {
        select(list.getOrNull(i), isStatic)
    }
}